package service

import (
	"gorm.io/gorm/clause"
	"wit-admin-go/system/app/dto"
	"wit-admin-go/system/app/model"
	"wit-admin-go/system/utils"
)

// PostCreateLog 创建日志
func (us *userService) PostCreateLog(log dto.Log) (err error) {
	return utils.DB.Create(&log).Error
}

// GetLogList 获取所有数据
func (us *userService) GetLogList(page *dto.LogPage) (*[]model.Log, int64) {
	sysLog := new([]model.Log)
	db := utils.DB.Preload(clause.Associations)
	if page.Path != "" {
		db = db.Where("path LIKE ?", "%"+page.Path+"%")
	}
	if page.Method != "" {
		db = db.Where("method = ?", page.Method)
	}
	if page.IpAddr != "" {
		db = db.Where("ip_addr LIKE ?", "%"+page.IpAddr+"%")
	}
	if page.City != "" {
		db = db.Where("city LIKE ?", "%"+page.City+"%")
	}
	if page.Os != "" {
		db = db.Where("os LIKE ?", "%"+page.Os+"%")
	}
	if page.Params != "" {
		db = db.Where("params = ?", page.Params)
	}
	if page.GroupName != "" {
		db = db.Where("group_name LIKE ?", "%"+page.GroupName+"%")
	}
	if page.Name != "" {
		db = db.Where("name LIKE ?", "%"+page.Name+"%")
	}
	if page.Status != 0 {
		db = db.Where("status = ?", page.Status)
	}
	total := db.Scopes(utils.Paginate(page.PageNum, page.PageSize)).Find(&sysLog).RowsAffected
	if total > 0 {
		return sysLog, total
	} else {
		return nil, 0
	}
}
